package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsTypeModel;
import com.wanxi.service.NewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Li
 * @Date 2023/7/8 18:29
 */
@RestController
@Api(tags = "新闻类型管理", value = "用户Open api 入口")
@RequestMapping("/back/newsType")
public class NewsTypeController {
    @Autowired
    private NewsTypeService newsTypeService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询所有新闻类型", response = NewsTypeModel.class, httpMethod = "GET")
    public ResultDto findAll(NewsTypeModel newsTypeModel) {
        Page<NewsTypeModel> page = new Page<>(newsTypeModel.getPage(), newsTypeModel.getLimit());
        QueryWrapper<NewsTypeModel> queryWrappers = query(newsTypeModel);
        IPage<NewsTypeModel> iPage = newsTypeService.page(page, queryWrappers);
        return ResultDto.builder()
                .code(0)
                .count((int) iPage.getTotal())
                .data(iPage.getRecords())
                .build();
    }


    /**
     * 用新闻修改和新增，获取新闻类型
     *
     * @return
     */
    @GetMapping("/findNewsType")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询可用新闻类型", response = NewsTypeModel.class, httpMethod = "GET")
    public ResultDto findNewsType() {
        QueryWrapper<NewsTypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable", 1);
        List<NewsTypeModel> newsTypeModelList = newsTypeService.list(queryWrapper);
        return ResultDto.builder()
                .code(0)
                .data(newsTypeModelList)
                .build();
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询新闻类型", response = NewsTypeModel.class, httpMethod = "GET")
    public ResultDto findById(NewsTypeModel newsTypeModel) {
        NewsTypeModel newsType = newsTypeService.getById(newsTypeModel.getId());
        return ResultDto.builder().code(0).data(newsType).build();
    }

    @PostMapping("/addNewsType")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增新闻类型", response = NewsTypeModel.class, httpMethod = "POST")
    public ResultDto addNewsType(NewsTypeModel newsTypeModel) {
        newsTypeService.save(newsTypeModel);
        return ResultDto.builder().code(0).msg("新增成功！").build();
    }

    @PostMapping("/updateNewsType")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改新闻类型", response = NewsTypeModel.class, httpMethod = "POST")
    public ResultDto updateNewsType(NewsTypeModel newsTypeModel) {
        newsTypeService.updateById(newsTypeModel);
        return ResultDto.builder().code(0).msg("修改成功！").build();
    }

    @PostMapping("/deleteNewsType")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除新闻类型", response = NewsTypeModel.class, httpMethod = "POST")
    public ResultDto deleteNewsType(NewsTypeModel newsTypeModel) {
        newsTypeService.removeById(newsTypeModel.getId());
        return ResultDto.builder().code(0).msg("删除成功").build();
    }

    private QueryWrapper<NewsTypeModel> query(NewsTypeModel newsTypeModel) {
        QueryWrapper<NewsTypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(newsTypeModel.getNewType() == null), "new_type", newsTypeModel.getNewType());
        queryWrapper.eq(newsTypeModel.getEnable() != null && newsTypeModel.getEnable() != -1, "enable", newsTypeModel.getEnable());
        return queryWrapper;
    }
}
